package com.youku.plugins.controlbarV4.view.components
{
   import flash.display.Sprite;
   import com.youku.baseUI.BasePlayerButton;
   import com.youku.baseUI.TweenSlider;
   import com.youku.utils.AssetUtil;
   import com.youku.utils.DrawUtil;
   import flash.display.DisplayObject;
   import fl.events.SliderEvent;
   import flash.events.MouseEvent;
   import com.youku.utils.ReportUtil;
   import com.youku.plugins.controlbarV4.view.events.SoundSliderEvent;
   import flash.filters.ColorMatrixFilter;
   
   public class SoundSlider extends Sprite
   {
      
      public function SoundSlider() {
         super();
         this.init();
         this._enabled = true;
      }
      
      protected var _data:Number;
      
      protected var _tempData:Number;
      
      protected var _mute:Boolean;
      
      protected var _enabled:Boolean;
      
      protected var _ball:BasePlayerButton;
      
      protected var _soundIcons:Array;
      
      protected var _enableLayer:Sprite;
      
      protected var _soundSlider:TweenSlider;
      
      public function init() : void {
         this.createItems();
         this.addEvents();
      }
      
      protected function createItems() : void {
         this._soundIcons = new Array();
         var _loc1_:BasePlayerButton = AssetUtil.getStyleButton("C_Sound_icon_0");
         AssetUtil.restoreButtonSize(_loc1_);
         this._soundIcons.push(_loc1_);
         _loc1_ = AssetUtil.getStyleButton("C_Sound_icon_1");
         AssetUtil.restoreButtonSize(_loc1_);
         this._soundIcons.push(_loc1_);
         _loc1_ = AssetUtil.getStyleButton("C_Sound_icon_2");
         AssetUtil.restoreButtonSize(_loc1_);
         this._soundIcons.push(_loc1_);
         this._ball = AssetUtil.getStyleButton("C_Sound_ball");
         AssetUtil.restoreButtonSize(this._ball);
         this._soundSlider = new TweenSlider();
         this._soundSlider.setStyle("sliderTrackSkin",AssetUtil.getAsset("C_Sound_slider"));
         this._soundSlider.setStyle("sliderTrackDisabledSkin",AssetUtil.getAsset("C_Sound_slider_disable"));
         this._soundSlider.setStyle("tickSkin",new Sprite());
         this._soundSlider.setStyle("thumbUpSkin",AssetUtil.getAsset("C_Sound_ball_up"));
         this._soundSlider.setStyle("thumbOverSkin",AssetUtil.getAsset("C_Sound_ball_over"));
         this._soundSlider.setStyle("thumbDownSkin",AssetUtil.getAsset("C_Sound_ball_down"));
         this._soundSlider.setStyle("thumbDisabledSkin",AssetUtil.getAsset("C_Sound_ball_disable"));
         this._soundSlider.updateSize();
         this._soundSlider.minimum = 0;
         this._soundSlider.maximum = 100;
         this._soundSlider.tickInterval = 1;
         this._soundSlider.liveDragging = true;
         this._enableLayer = DrawUtil.RectangleSprite(0,0,20,20,39372,0,null,0);
         this._enableLayer.visible = false;
         addChild(this._soundSlider);
         var _loc2_:* = 0;
         while(_loc2_ < this._soundIcons.length)
         {
            addChild(this._soundIcons[_loc2_] as DisplayObject);
            _loc2_++;
         }
         addChild(this._enableLayer);
         this._soundSlider.x = DisplayObject(this._soundIcons[0]).width;
         this._soundSlider.y = (DisplayObject(this._soundIcons[0]).height - this._soundSlider.height) / 2;
         this._enableLayer.x = this._enableLayer.y = 0;
         this._enableLayer.width = _loc1_.width + this._soundSlider.width + 8;
         this._enableLayer.height = _loc1_.height;
         this._ball.x = this._enableLayer.x + this._enableLayer.width;
         this._ball.y = 2;
      }
      
      protected function addEvents() : void {
         var _loc1_:DisplayObject = null;
         this._soundSlider.addEventListener(SliderEvent.CHANGE,this.onSoundChange);
         this._soundSlider.addEventListener(SliderEvent.THUMB_RELEASE,this.onThumbRelease);
         for each(_loc1_ in this._soundIcons)
         {
            _loc1_.addEventListener(MouseEvent.CLICK,this.onSoundIconClick);
         }
      }
      
      private function onThumbRelease(param1:SliderEvent) : void {
         ReportUtil.sendUserActionLog(ReportUtil.VOLUME_CONTROL_SLIDER_DRAG);
      }
      
      protected function onSoundIconClick(param1:MouseEvent) : void {
         var _loc2_:* = false;
         if(param1.currentTarget == this._soundIcons[0])
         {
            _loc2_ = false;
         }
         else
         {
            _loc2_ = true;
         }
         this.setMute(_loc2_);
         if(_loc2_)
         {
            ReportUtil.sendUserActionLog(ReportUtil.VOLUME_CONTROL_MUTE_OPEN_CLICK);
         }
         else
         {
            ReportUtil.sendUserActionLog(ReportUtil.VOLUME_CONTROL_MUTE_CANCEL_CLICK);
         }
         var _loc3_:SoundSliderEvent = new SoundSliderEvent(SoundSliderEvent.MUTE_CHANGED);
         _loc3_.mute = _loc2_;
         dispatchEvent(_loc3_);
         _loc3_ = new SoundSliderEvent(SoundSliderEvent.CHANGED);
         if(!this._mute)
         {
            if(this.data == 0)
            {
               this.data = 0.5;
            }
         }
         _loc3_.volume = this._mute?0:this.data;
         _loc3_.record = false;
         dispatchEvent(_loc3_);
      }
      
      protected function onSoundChange(param1:SliderEvent) : void {
         var _loc3_:SoundSliderEvent = null;
         if(this._mute)
         {
            this._mute = false;
            _loc3_ = new SoundSliderEvent(SoundSliderEvent.MUTE_CHANGED);
            _loc3_.mute = false;
            dispatchEvent(_loc3_);
         }
         var _loc2_:Number = this._soundSlider.value / 100;
         if(this.data > 1 && param1 == null)
         {
            _loc2_ = this.data;
         }
         _loc3_ = new SoundSliderEvent(SoundSliderEvent.CHANGED);
         _loc3_.volume = _loc2_;
         _loc3_.record = _loc2_ <= 1?true:false;
         this.data = _loc2_;
         dispatchEvent(_loc3_);
      }
      
      protected function setSoundFrame() : void {
         var _loc1_:* = 0;
         while(_loc1_ < this._soundIcons.length)
         {
            this._soundIcons[_loc1_].visible = false;
            _loc1_++;
         }
         var _loc2_:* = 0;
         var _loc3_:Number = this._mute?0:this.data;
         if(_loc3_ == 0)
         {
            _loc2_ = 0;
         }
         else if(_loc3_ > 0 && _loc3_ < 0.5)
         {
            _loc2_ = 1;
         }
         else if(_loc3_ >= 0.5)
         {
            _loc2_ = 2;
         }
         
         
         this._soundIcons[_loc2_].visible = true;
      }
      
      public function setMute(param1:Boolean) : void {
         this._mute = param1;
         if(param1)
         {
            this._tempData = this.data;
            this._soundSlider.value = 0;
         }
         else
         {
            this.data = this._tempData;
            this._soundSlider.value = this.data * 100;
         }
         this.setSoundFrame();
      }
      
      public function set data(param1:Number) : void {
         this.setDisplayData(param1);
         this._tempData = this._data;
      }
      
      public function get data() : Number {
         return this._data;
      }
      
      public function setDisplayData(param1:Number) : void {
         this._data = param1;
         this._soundSlider.value = this._data * 100;
         this.setSoundFrame();
      }
      
      public function set mute(param1:Boolean) : void {
         this.setMute(param1);
      }
      
      public function get mute() : Boolean {
         return this._mute;
      }
      
      public function increase(param1:Boolean) : void {
         var _loc2_:Number = this._data;
         var _loc3_:Number = _loc2_ >= 1?0.5:0.1;
         _loc2_ = _loc2_ + _loc3_;
         _loc2_ = _loc2_ > 5?5:_loc2_;
         if(_loc2_ > 1 && _loc2_ < 1.5)
         {
            _loc2_ = 1;
         }
         this.data = _loc2_;
         if(param1)
         {
            this.onSoundChange(null);
         }
      }
      
      public function decrease(param1:Boolean) : void {
         var _loc2_:Number = this._data;
         var _loc3_:Number = _loc2_ > 1?0.5:0.1;
         _loc2_ = _loc2_ - _loc3_;
         _loc2_ = _loc2_ < 0?0:_loc2_;
         this.data = _loc2_;
         if(param1)
         {
            this.onSoundChange(null);
         }
      }
      
      override public function get height() : Number {
         return DisplayObject(this._soundIcons[0]).height;
      }
      
      override public function get width() : Number {
         return DisplayObject(this._soundIcons[0]).width + this._soundSlider.width + 8;
      }
      
      public function get enabled() : Boolean {
         return this._enabled;
      }
      
      public function set enabled(param1:Boolean) : void {
         this._enabled = param1;
         if(this._enableLayer)
         {
            this._enableLayer.visible = !this._enabled;
         }
         if(this._soundSlider)
         {
            this._soundSlider.enabled = this._enabled;
         }
      }
      
      protected function filterUI(param1:Boolean) : void {
         var _loc2_:ColorMatrixFilter = new ColorMatrixFilter([0,0,0,0,114,0,0,0,0,114,0,0,0,0,114,0,0,0,1,1]);
         var _loc3_:* = 0;
         if(this._enabled)
         {
            _loc3_ = 0;
            while(_loc3_ < this._soundIcons.length)
            {
               this._soundIcons[_loc3_].filters = [];
               _loc3_++;
            }
         }
         else
         {
            _loc3_ = 0;
            while(_loc3_ < this._soundIcons.length)
            {
               this._soundIcons[_loc3_].filters = [_loc2_];
               _loc3_++;
            }
         }
      }
      
      public function sendSoundChangedEvent() : void {
         var _loc1_:SoundSliderEvent = new SoundSliderEvent(SoundSliderEvent.CHANGED);
         _loc1_.volume = this._mute?0:this.data;
         _loc1_.record = false;
         _loc1_.userAction = false;
         dispatchEvent(_loc1_);
      }
   }
}
